package com.um.weixin.pay.config;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Binary Wang
 */
@Configuration
@ConditionalOnClass(WxPayService.class)
@EnableConfigurationProperties(WxPayProperties.class)
@AllArgsConstructor
@Slf4j
public class WxPayConfiguration {

  private WxPayProperties properties;

  @Bean
  @ConditionalOnMissingBean
  public WxPayService wxService() {
    WxPayConfig payConfig = new WxPayConfig();
    payConfig.setAppId(StringUtils.trimToNull(this.properties.getAppId()));
    payConfig.setSubAppId(StringUtils.trimToNull(this.properties.getSubAppId()));
    payConfig.setMchId(StringUtils.trimToNull(this.properties.getMchId()));

    payConfig.setMchKey(StringUtils.trimToNull(this.properties.getMchKey()));
    payConfig.setEntPayKey(StringUtils.trimToNull(this.properties.getEntPayKey()));
    payConfig.setSubMchId(StringUtils.trimToNull(this.properties.getSubMchId()));
    payConfig.setNotifyUrl(StringUtils.trimToNull(this.properties.getNotifyUrl()));

    payConfig.setTradeType(this.properties.getTradeType());
    payConfig.setSignType(this.properties.getSignType());

    payConfig.setKeyString(this.properties.getKeyString());
    payConfig.setKeyPath(this.properties.getKeyPath());

    payConfig.setPrivateKeyString(this.properties.getPrivateKeyString());
    payConfig.setPrivateKeyPath(this.properties.getPrivateKeyPath());

    payConfig.setPrivateCertString(this.properties.getPrivateCertString());
    payConfig.setPrivateCertPath(this.properties.getPrivateCertPath());

    payConfig.setApiV3Key(StringUtils.trimToNull(this.properties.getApiV3Key()));
    payConfig.setCertSerialNo(StringUtils.trimToNull(this.properties.getCertSerialNo()));
    payConfig.setServiceId(StringUtils.trimToNull(this.properties.getServiceId()));
    payConfig.setPayScoreNotifyUrl(this.properties.getPayScoreNotifyUrl());
    payConfig.setPayScorePermissionNotifyUrl(this.properties.getPayScorePermissionNotifyUrl());

    payConfig.setPublicKeyId(StringUtils.trimToNull(this.properties.getPublicKeyId()));
    payConfig.setPublicKeyString(StringUtils.trimToNull(this.properties.getPublicKeyString()));
    payConfig.setPublicKeyPath(StringUtils.trimToNull(this.properties.getPublicKeyPath()));

    // 可以指定是否使用沙箱环境
    payConfig.setUseSandboxEnv(false);

    WxPayService wxPayService = new WxPayServiceImpl();
    try {
      wxPayService.setConfig(payConfig);
    } catch (Exception e) {
      log.warn("微信支付配置初始化错误：{}", e.getMessage());
    }
    return wxPayService;
  }

}
